<template>
    <IModal v-model="defaultValue" :width="400" :loading="loading">
        <Form :model="data" :rules="ruleValidate" ref="formCreate">
            <FormItem label="存储条件" prop="condition">
                <RemoteSelect url="common/select/dictionaries/PRODUCT_CONDITION"
                              v-model="data.condition"></RemoteSelect>
            </FormItem>
            <FormItem label="需要进行效期管理" prop="validity">
                <RemoteSelect url="common/select/dictionaries/PRODUCT_VALIDITY" @on-change="validityChange"
                              v-model="data.validity"></RemoteSelect>
            </FormItem>
            <FormItem label="保质期禁售天数" prop="lockup_validity">
                <Input v-model="data.lockup_validity" number></Input>
            </FormItem>
        </Form>
        <div slot="footer">
            <Button type="primary" icon="ios-add" @click="submit('formCreate')">提交</Button>
        </div>
    </IModal>
</template>

<script>
    import IModalMixins from "../../../../mixins/iDrawerMixins";
    import IModal from "../../../../components/layout/IModal";
    import RemoteSelect from "../../../../components/form/remoteSelect";

    export default {
        name: "iSimpleUpdate",
        components: {RemoteSelect, IModal},
        mixins: [IModalMixins],
        data() {
            return {
                data: {},
                ruleValidate: {
                    condition: [{required: true, message: "存储条件不能为空"}],
                    validity: [{required: true, message: "产品效期管理不能为空"}],
                    lockup_validity: [{required: true, type: 'integer', message: "保质期禁售天数必须填写"}]
                }
            }
        },
        mounted() {
            this.getData();
        },
        methods: {
            getData() {
                this.loading = true;
                this.$http.get(`product/index/${this.props.id}/edit`).then((res) => {
                    this.data = res;
                }).finally(() => {
                    this.loading = false;
                })
            },
            submit(name) {
                this.validate(name).then(() => {
                    this.loading = true;
                    this.$http.put(`product/index/${this.props.id}/simple`, this.data).then(() => {
                        this.defaultValue = false;
                    }).finally(() => {
                        this.loading = false;
                    });
                }).catch();
            },
            validityChange(val) {
                if (val === '1') {
                    this.data.lockup_validity = 0;
                }
            }
        }
    }
</script>

<style scoped>

</style>
